package com.tanhua.dubbomongo.api;

import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.dubbointerface.api.RecommendUserApi;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 查询今日佳人
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //创建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //创建Query对象,降序排列，只显示第一个数据
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //用mongoTemplate调用find方法获取RecommendUser对象
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    /**
     * 推荐的人，返回PageResult对象
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //创建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //创建Query对象,降序排列，
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(pagesize).skip((page-1)*pagesize);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);
        PageResult pr = new PageResult(page,pagesize,count,list);
        return pr;
    }
}
